<template>
  <!-- 导航部分开始 -->
  <!--    <el-row style="margin-top: 10px;">-->
  <!--        <el-col :span="24">-->
  <!--            <el-breadcrumb separator="/">-->
  <!--                <el-breadcrumb-item>首页</el-breadcrumb-item>-->
  <!--                <el-breadcrumb-item>设备类型管理</el-breadcrumb-item>-->
  <!--            </el-breadcrumb>-->
  <!--        </el-col>-->
  <!--    </el-row>-->
  <!-- 导航部分结束 -->
  <!-- 搜索栏部分开始 -->
    <el-row style="margin-top: 10px;">
        <el-col :span="24">
            <!-- model 绑定表单数据 -->
            <el-form :inline="true" :model="querySubject" class="demo-form-inline">
                <el-form-item label="项目编码">
                    <el-input v-model="querySubject.subjectCode" placeholder="请输入项目编码"/>
                </el-form-item>
                <el-form-item label="项目名称">
                    <el-input v-model="querySubject.subjectName" placeholder="请输入项目名称"/>
                </el-form-item>
                <el-form-item label="项目类型">
                    <el-select
                            style="width: 150px"
                            v-model="querySubject.subjectType"
                            placeholder="请选择项目类型"
                    >
                        <el-option value="" label="全部"/>
                        <el-option value="设备点检" label="设备点检"/>
                        <el-option value="设备保养" label="设备保养"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="loadSubject">搜索</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
  <!-- 搜索栏部分结束 -->

  <!-- 工具栏部分开始 -->
    <el-row style="margin-top: 10px;">
        <el-col :span="24">
            <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="新增点检保养计划"
                    placement="top-start"
            >
                <el-button @click="toAddSubject" type="primary">
                    <el-icon>
                        <Plus/>
                    </el-icon>
                </el-button>
            </el-tooltip>
            <el-button @click="editBtnHandler" :disabled="editBtnDisabled" type="primary">
                <el-icon>
                    <EditPen/>
                </el-icon>
            </el-button>
            <el-popconfirm confirm-button-text="是" cancel-button-text="否"
                           @confirm="deleteBtnHandler" title="您确定要删除选中的点检保养计划吗？">
                <template #reference>
                    <el-button :disabled="deleteBtnDisabled" type="primary">
                        <el-icon>
                            <Minus/>
                        </el-icon>
                    </el-button>
                </template>
            </el-popconfirm>
            <el-button @click="exportSubject" type="primary">
                <el-icon>
                    <Download/>
                </el-icon>
            </el-button>
            <!--            <el-button type="primary">-->
            <!--                <el-icon>-->
            <!--                    <Upload/>-->
            <!--                </el-icon>-->
            <!--            </el-button>-->
            <!--            <el-button type="primary">-->
            <!--                <el-icon>-->
            <!--                    <Download/>-->
            <!--                </el-icon>-->
            <!--            </el-button>-->
        </el-col>
    </el-row>
  <!-- 工具栏部分结束 -->
  <!-- 列表部分开始 -->
    <el-row style="margin-top: 10px;">
        <el-col :span="24">
            <el-table ref="subjectTaebl" @selection-change="handleSelectionChange" :data="subjects" border :stripe="true" style="width: 95%">
                <el-table-column type="selection" width="55"/>
                <!--<el-table-column prop="subjectCode" label="项目编码">-->
                <!--    <template #default="scope">-->
                <!--        <el-link @click="showToolItem(scope.row.toolId)" type="primary">{{ scope.row.toolCode }}-->
                <!--        </el-link>-->
                <!--    </template>-->
                <!--</el-table-column>-->
                <el-table-column prop="subjectCode" label="项目编码"/>
                <el-table-column prop="subjectName" label="项目名称"/>
                <el-table-column prop="subjectType" label="项目类型"/>
                <el-table-column prop="subjectContent" label="项目内容"/>
                <el-table-column prop="subjectStandard" label="标准"/>
                <el-table-column prop="enableStatus" label="状态">
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.enableStatus == 1">是</el-tag>
                        <el-tag type="warning" v-else-if="scope.row.enableStatus == 2">否</el-tag>
                        <el-tag type="danger" v-else>删除</el-tag>
                    </template>
                </el-table-column>
                <!-- 行工具栏 -->
                <el-table-column width="140" label="操作">
                    <template #default="scope">
                        <el-button type="primary" @click="toEditSubject(scope.row.subjectId)" size="small">编辑</el-button>
                        <el-popconfirm confirm-button-text="是" cancel-button-text="否"
                                       @confirm="deleteSubject(scope.row.subjectId)" title="您确定要删除这个设备点检保养项目吗？">
                            <template #reference>
                                <el-button type="danger" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </el-col>
    </el-row>
  <!-- 列表部分结束 -->
  <!-- 分页部分开始 -->
    <el-row style="margin-top: 10px;">
        <el-col :span="24">
            <el-pagination background @change="pageChange" layout="prev, pager, next" v-bind:page-size="limit" :total="total"/>
        </el-col>
    </el-row>
  <!-- 分页部分结束 -->

  <!-- 使用子组件 -->
    <SubjectAddWin :visible="addSubjectVisble"  @optionCompleted="addOptionCompleted"/>
    <SubjectEditWin :visible="editSubjectVisble"  :subjectId="editSubjectId" @optionCompleted="editOptionCompleted"/>
  <!--  <SubjectItem :visible="showSubjectVisble" :toolId="showToolId" @optionCompleted="showOptionCompleted"/>-->
</template>

<script>
import SubjectAddWin from './subject-add-win.vue';
import SubjectEditWin from './subject-edit-win.vue';
// import SubjectItem from './subject-item-win.vue';
// 引入api
import subjectRequest from '../../../api/subject.js';
import config from "@/util/config.js";

export default {
    components: {
        SubjectAddWin,
        SubjectEditWin,
        // ToolItem
    },
    name: "Subject.vue",
    data() {
        return {
            querySubject: {},// 绑定查询条件
            createTimes: [],// 绑定搜索栏中的时间范围
            subjects: [],// 列表数据
            // toolTypes:[],
            currentPage: 1,// 当前页码
            limit: 10,// 每页大小
            total: 0,// 总条数
            addSubjectVisble: false,// 新增设备类型的弹窗是否显示

            editSubjectVisble: false,// 标记是否显示编辑设备类型的弹窗
            editSubjectId: -1,// 编辑设备类型的id

            showSubjectVisble: false,// 标记是否显示设备类型项的弹窗
            currentSubjectId: -1,// 当前设备类型的type

            showSubjectId:-1,

            editBtnDisabled: true,
            deleteBtnDisabled: true
        }
    }, methods: {
        exportSubject() {
            // 超链接下载导出文件
            // 拼接请求参数
            let url = config.dvUrl + "/dv/subject/export?a=1";
            for (let paramName in this.querySubject) {
                if (this.querySubject[paramName]) {
                    url += "&" + paramName + "=" + this.querySubject[paramName];
                }
            }
            url += "&page=" + this.currentPage + "&limit=" + this.limit;
            //超链接请求
            location.href = url;
        },
        // 重置查询条件表单
        resetForm(){
            this.querySubject={};
        },
        /**
         * 工具栏删除按钮事件
         */
        deleteBtnHandler() {
            let selectionRows = this.$refs.subjectTaebl.getSelectionRows();
            let subjectIds = new Array();
            selectionRows.forEach(row => {
                subjectIds.push(row.subjectId);
            })
            subjectRequest.deleteSubjects(subjectIds, res => {
                this.$message({
                    type: 'success',
                    message: '删除工装夹具成功!'
                });
                this.loadSubject();
            });
        },
        /**
         * 行删除按钮事件
         * @param dictId
         */
        deleteSubject(subjectId) {
            subjectRequest.deleteSubject(subjectId, res => {
                this.$message({
                    type: 'success',
                    message: '删除工装夹具成功!'
                });
                this.loadSubject();
            });
        },
        /**
         * 工具栏编辑按钮事件
         */
        editBtnHandler() {
            let editSubject = this.$refs.subjectTaebl.getSelectionRows()[0];
            if (editSubject != undefined) {
                this.editSubjectId = editSubject.subjectId;
                this.editSubjectVisble = true;
            } else {
                this.$message({
                    type: 'warning',
                    message: '请选择要编辑的设备类型!'
                });
            }
        },
        /**
         * 表格中数据选择事件
         */
        handleSelectionChange() {
            let selectionRows = this.$refs.subjectTaebl.getSelectionRows();
            if (selectionRows.length == 0) {
                this.editBtnDisabled = true;
                this.deleteBtnDisabled = true;
            } else if (selectionRows.length == 1) {
                this.editBtnDisabled = false;
                this.deleteBtnDisabled = false;
            } else {
                this.editBtnDisabled = true;
                this.deleteBtnDisabled = false;
            }
        },
        /**
         * 设备类型列表中设备类型类别的超链接事件
         * @param dict
         */
        // showToolItem(toolId) {
        //     this.showToolId = toolId;
        //     // 弹窗
        //     this.showToolVisble = true;
        //     // this.dictItemVisible = true;
        // },
        /**
         * 监听子组件设备类型项弹窗的操作是否完成
         */
        // showOptionCompleted() {
        //     // 将弹窗设置为false
        //     this.showToolVisble = false;
        //     // 重新加载列表
        //     this.loadTool();
        // },
        /**
         * 监听编辑设备类型的子组件操作完成的事件
         */
        editOptionCompleted() {
            // 将弹窗设置为false
            this.editSubjectVisble = false;
            // 重新加载列表
            this.loadSubject();
        },
        /**
         * 行编辑按钮事件
         */
        toEditSubject(subjectId) {
            // 将传入的subjectId设置到editsubjectId
            this.editSubjectId = subjectId;
            // 弹出编辑窗体
            this.editSubjectVisble = true;
        },
        /**
         * 工具栏的新增设备类型按钮事件
         */
        toAddSubject() {
            // 弹窗 addSubjectVisble设置为true
            this.addSubjectVisble = true;
        },
        /**
         * 监听添加设备类型的子组件操作完成的事件
         */
        addOptionCompleted() {
            // 将弹窗设置为false
            this.addSubjectVisble = false;
            // 重新加载列表
            this.loadSubject();
        },
        /**
         * 翻页的事件
         * @Param currentPage 最新的页码
         */
        pageChange(currentPage, pageSize) {
            // 将最新的页码赋值给this.currentPage
            this.currentPage = currentPage;
            // 重新加载数据
            this.loadSubject();
        },
        // queryToolTypeNames(){
        //     toolRequest.queryToolTypeNames(res => {
        //         this.toolTypes = res.data;
        //     })
        // },
        /**
         * 加载设备类型信息
         */
        loadSubject() {
            // 设置其他的条件
            this.querySubject.page = this.currentPage;
            this.querySubject.limit = this.limit;
            // console.log(this.createTimes);
            // 设置时间
            if (this.createTimes != undefined && this.createTimes.length > 0)
                this.querySubject.createTimeStart = this.createTimes[0];
            if (this.createTimes != undefined && this.createTimes.length > 1)
                this.querySubject.createTimeEnd = this.createTimes[1];
            subjectRequest.loadSubject(this.querySubject, res => {
                this.subjects = res.data.data;
                this.total = res.data.total;
            })
        }
    }, mounted() {
        this.loadSubject();
        // this.queryToolTypeNames();
    }
}
</script>

<style scoped>

</style>